<?php
namespace Home\Controller;
use Think\Controller;
class MemberController extends Controller 
{
    public function index(){

        $this->display();
    }

    //ajax登录信息处理返回请求
    public function ajaxChkLogin() {
        if (session('m_id'))
            echo json_encode(array(
                'login' => 1,
                'username' => session('m_username'),
            ));
        else
            echo json_encode(array(
                'login' => 0,
            ));
    }

    public function chkcode(){
        $Verify = new \Think\Verify(array(
            'fontSize' => 40,
            'length'  => 4,
            'userNoise' => false,
        ));
        $Verify->entry();
    }

    public function login() {
        if (IS_POST) {
            $model = D('Admin/Member');
            if ($model->validate($model->_login_validate)->create()){
                //$needPassword = false
                if ($model->login()){
                    //默认跳转地址
                    $returnUrl = U('/');
                    //如果session中有一个要跳转的地址就跳过去
                    $ru = session('returnUrl');
                    if ($ru){
                            session('returnUrl',null);
                        $returnUrl = $ru;
                    }
                    $this->success('登录成功!',$returnUrl);
                    exit;
                }
            }
            $this->error($model->getError());
        }
        $this->assign(array(
            '_page_title' => '登录',
            '_page_keywords' => '登录',
            '_page_description' => '登录'
        ));

        $this->display();
    }

    public function regist() {
        if (IS_POST){
            $model = D('Admin/Member');
            if ($model->create(I('post.'),1)){
                if ($model->add()){

                    $model->username = I('post.username');
                    $model->login(false);
                    $this->success('注册成功!',U('/'));
                    exit;
                }
            }
            $this->error($model->geterror());
        }
        $this->assign(array(
            '_page_title' => '注册',
            '_page_keywords' => '注册',
            '_page_description' => '注册',
        ));
        $this->display();
    }

    public function logout() {
        session(null);
        redirect('/');
    }
}